package com.hqj.demo;

import java.io.File;
import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * 异常
 */
public class Test01 {
    public static void main(String[] args) throws Exception{
        ExecClass ec = new ExecClass();
        ec.test1();

        ec.test2();

        ec.test3(-1);
    }
}

class ExecClass{

    public void test1(){
        try {
            String str = null;
            if ("abc".equals(str)) {
                System.out.println("某个值");
            }
            Scanner scan = new Scanner(System.in);
            int a = scan.nextInt();
            int b = scan.nextInt();
            int c = a/b;
        }catch (InputMismatchException e){
            e.printStackTrace();
        }catch (RuntimeException e){//捕捉发生的异常
            System.out.println("发生空指针异常!"+e.getMessage());
            //e.printStackTrace();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            System.out.println("不管是否发生异常都要执行的地方");
        }
    }

    public void test2() throws  Exception{

        File f = new File("d:/test");
        if(!f.exists()){
            f.createNewFile();

        }
    }

    public void test3(int age) throws Exception {
        if(age>100||age<1){
            throw new NullPointerException("年龄不能小于1或不能大于100");
        }
    }

}